SQL result container class
Sorm4j treats Java class which is satisfied the following rules is regarded as SQL result container class (these rules are stated in the preferred order). * A column name in SQL result and a field accessor name of the container object are compared based on canonical case. We assume the follwing customer table in example: create table customer (id int primary key, name varchar)
/yuunkjm/--.icon
code:Example of Record.java
@OrmRecord
public record Customer(int id, String name) { }
@OrmRecord indicates the class has the canonical record contractor and uses it. The columns in SQL results are mapped to component names of the Record class.
/yuunkjm/--.icon
Rule 2: Java class has public constructor annotated by @OrmConstructor and the types of fields are supported returned types. code:Example of OrmConstructor.java
public class Customer {
private int id;
private String name;
@OrmConstructor({"id", "name"})
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
}
@OrmConstructor indicates the constructor which sets the result from the database.
/yuunkjm/--.icon
Rule 3: Java class has the default constructor (public no-arg constructor) and the types of public accessors (fields or methods) corresponding column are supported returned types. code:Example1 of rule3.java
public class Customer {
public int id;
public String name;
}
code:Example2 of rule3.java
public class Customer {
private int id;
private String name;
public Customer(int id, String name);
private setId(int id){this.id=id;}
private setName(String name){this.name=name;}
}
Columns are mapped fields accessors of container class with the following rules.
Rule 1: The method annotated @OrmSetter (field accessor name is the value of annotation)
Rule 2: The field annotated @OrmColumn (field accessor name is the value of annotation)
Rule 3: setX method (field accessor name is trim set from setter/getter)
Rule 4: Direct field access (field accessor name is the filed name)